package com.pkk.penguin.frame.know.java.Javacoreknowledge.thread.threaduse;

import org.junit.Test;

/**
 * @description: 线程的简单使用技巧
 * @author: peikunkun
 * @create: 2020-01-10 14:32
 **/
public class ThreadUseSimple {


  private final Object lock = new Object();


  @Test
  @SuppressWarnings("all")
  public void Administrator_14_20200113155259() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_14()_20200113155259】");
    System.out.println("此方法测试描述：【】");
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        final Thread thread = Thread.currentThread();
        System.out.println(thread.getName() + " will WAITING");
        synchronized (thread) {
//            thread.wait();
        }
        System.out.println(thread.getName() + " is Running");
      }
    });
    thread1.start();

    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        final Thread thread = Thread.currentThread();
        System.out.println(thread.getName() + " will WAITING");
        synchronized (thread) {
//            thread.wait();
        }
        System.out.println(thread.getName() + " is Running");
      }
    });
    thread.start();
//    thread.notifyAll();
    thread.join();
    thread1.join();
    Thread.sleep(1000L);
  }


  /**
   * @Description: thread.join()等待thread线程执行完成, 当前线程进入阻塞状态
   * @Author: peikunkun
   * @Date: 2020/1/13 0013 下午 3:51
   */
  @Test
  @SuppressWarnings("all")
  public void Administrator_11_20200110143338() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_11()_20200110143338】");
    System.out.println("此方法测试描述：【Thread.join()\n"
        + "1:Thread.join()等待当前线程终止\n"
        + "2:Thread.join(long time)让当前线程进入TIMED_WAITING(等待状态)[Thread.join()让当前线程进入WAOTING]\n"
        + "3:一般地我们可以使用thread-simple-1.join()让thread-simple-1线程(子线程)】\n"
        + "【重点:在当前线程中调用另外一个线程(b)的join方法,则当前线程会变为阻塞状态(waiting),待另外一个线程(b)执行结束之后,当前线程从阻塞状态变为就绪状态(READY)】");

    Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(1000L);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("子线程执行结束");
      }
    });
    t.setName("thread-join-simple-1");
    System.out.println(t.getName() + "线程状态:" + t.getState());
    System.out.println(Thread.currentThread().getName() + "线程状态:" + t.getState());
    t.start();
    System.out.println(t.getName() + "线程状态:" + t.getState());
    System.out.println(Thread.currentThread().getName() + "线程状态:" + t.getState());
    //当前主线程main进入阻塞状态,等待t线程执行完成,然后主线程main进入就绪状态
    t.join();
    System.out.println(t.getName() + "线程状态:" + t.getState());
    System.out.println(Thread.currentThread().getName() + "线程状态:" + t.getState());
    System.out.println("线程执行结束");

  }


}
